package com.ruoyi.inventory.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.inventory.domain.vo.QueryDeliverVo;
import com.ruoyi.inventory.domain.vo.QueryProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.inventory.domain.Delivery;
import com.ruoyi.inventory.service.IDeliveryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 配货单Controller
 * 
 * @author xwj
 * @date 2024-11-10
 */
@RestController
@RequestMapping("/inventory/delivery")
@Api(tags = "配货单")
public class DeliveryController extends BaseController {
    @Autowired
    private IDeliveryService deliveryService;

    /**
     * 查询配货单列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Delivery delivery) {
        startPage();
        List<Delivery> list = deliveryService.selectDeliveryList(delivery);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Delivery delivery) {
        startOrderBy();
        List<Delivery> list = deliveryService.selectDeliveryList(delivery);
        return success(list);
    }

    /**
     * 导出配货单列表
     */
    @Log(title = "配货单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Delivery delivery) {
        List<Delivery> list = deliveryService.selectDeliveryList(delivery);
        ExcelUtil<Delivery> util = new ExcelUtil<Delivery>(Delivery.class);
        util.exportExcel(response, list, "配货单数据");
    }

    /**
     * 获取配货单详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(deliveryService.selectDeliveryById(id));
    }

    /**
     * 新增配货单
     */
    @Log(title = "配货单", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Delivery delivery) {
        SysUser user = getLoginUser().getUser();
        if(delivery.getId() == null) {
        delivery.setCreateId(user.getUserId());
            delivery.setCreateTime(DateUtils.getNowDate());
            delivery.setCreateBy(user.getNickName());
            deliveryService.insertDelivery(delivery);
        } else {
            delivery.setUpdateId(user.getUserId());
            delivery.setUpdateTime(DateUtils.getNowDate());
            delivery.setUpdateBy(user.getNickName());
            deliveryService.updateDelivery(delivery);
        }

        return success(delivery);
    }

    /**
     * 修改配货单
     */
    @Log(title = "配货单", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Delivery delivery) {
        SysUser user = getLoginUser().getUser();
                delivery.setUpdateId(user.getUserId());
                delivery.setUpdateTime(DateUtils.getNowDate());
                delivery.setUpdateBy(user.getNickName());
        return success(deliveryService.updateDelivery(delivery));
    }

    /**
     * 删除配货单
     */
    @Log(title = "配货单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(deliveryService.deleteDeliveryByIds(ids));
    }

    @Log(title = "配货单", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Delivery delivery) {
        int num = deliveryService.delete(delivery);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Delivery>> queryPage(@RequestBody Delivery delivery) {
        Pages<Delivery> page = deliveryService.queryPage(delivery);
        return ResponseModel.of(page);
    }

    @GetMapping("/statistics")
    @ApiOperation("供货单-统计")
    public AjaxResult statistics(String year, Long customerId) {
        List<Map> rs = deliveryService.statistics(year, customerId);
        return success(rs);
    }

    @PostMapping("/report")
    @ApiOperation("供货单-统计")
    public AjaxResult report(@RequestBody QueryDeliverVo queryDeliverVo) {
        List<Map> rs = deliveryService.report(queryDeliverVo);
        return success(rs);
    }

    @PostMapping("/reportDay")
    @ApiOperation("供货单-统计")
    public AjaxResult reportDay(@RequestBody QueryDeliverVo queryDeliverVo) {
        List<Map> rs = deliveryService.reportDay(queryDeliverVo);
        return success(rs);
    }

    @GetMapping("/reportCustomer")
    @ApiOperation("供货单-客户-统计")
    public AjaxResult reportCustomer(String year, Long customerId) {
        List<Map> rs = deliveryService.reportCustomer(year, customerId);
        return success(rs);
    }

    @GetMapping("/reportCustomerDay")
    @ApiOperation("供货单-客户-统计")
    public AjaxResult reportCustomerDay(String startTime, String endTime, Long customerId) {
        List<Map> rs = deliveryService.reportCustomerDay(startTime, endTime, customerId);
        return success(rs);
    }

    @PostMapping("/reportCost")
    @ApiOperation("成本-统计")
    public AjaxResult reportCost(@RequestBody QueryDeliverVo queryDeliverVo) {
        List<Map> rs = deliveryService.reportCost(queryDeliverVo);
        return success(rs);
    }

    @PostMapping("/reportGood")
    @ApiOperation("商品-统计")
    public AjaxResult reportGood(@RequestBody QueryDeliverVo queryDeliverVo) {
        List<Map> rs = deliveryService.reportGood(queryDeliverVo);
        return success(rs);
    }

    @PostMapping("/reportBill")
    @ApiOperation("商品-对账单")
    public AjaxResult reportBill(@RequestBody QueryDeliverVo queryDeliverVo) {
        List<Map> rs = deliveryService.reportBill(queryDeliverVo);
        return success(rs);
    }

    @PostMapping("/reportPrice")
    @ApiOperation("商品-对账单总价")
    public AjaxResult reportPrice(@RequestBody QueryDeliverVo queryDeliverVo) {
        List<Map> rs = deliveryService.reportPrice(queryDeliverVo);
        return success(rs);
    }


    @PostMapping("/generateShopList")
    @ApiOperation("根据货单，分组查询商品")
    public AjaxResult generateShopList(@RequestBody QueryProductVo queryProductVo) {
         List<Map> rs = deliveryService.generateShopList(queryProductVo);
        return success(rs);
    }

    @PostMapping("/infos")
    @ApiOperation("根据Id获取多个货单号")
    public AjaxResult infos(@RequestBody QueryProductVo queryProductVo) {
        Long []ids = queryProductVo.getIds();

        List<Delivery> list = Lists.newArrayList();
        for(Long id: ids) {
            Delivery delivery = deliveryService.selectDeliveryById(id);
            list.add(delivery);
        }
        return success(list);
    }

}
